(function(){

YAHOO.pendular.WindowManager = function(){
    var me = this;
    me.activeEl = "pendular-main";
    me.windowList = {};
    me.windowList[me.activeEl] = me.head = me.tail = {
        key: me.activeEl
    };
    me.head.next = me.head;
    me.head.prev = me.head;
    me.onOpenWindow = new YAHOO.util.CustomEvent("openWindow");
    me.onCloseWindow = new YAHOO.util.CustomEvent("closeWindow");
    me.onActivate = new YAHOO.util.CustomEvent("activate");
};

YAHOO.lang.extend(YAHOO.pendular.WindowManager, Object, {
    setVisible: function(el, setting){
        YAHOO.util.Dom.get(el).style.display = setting ? "" : "none";
    },
    setActiveEl: function(el) {
        var me = this, windowListEntry;
        if (el===me.activeEl){
            return;
        }
        me.onActivate.fire(me.activeEl, el);
        me.setVisible(me.activeEl, false);
        me.setVisible(el, true);
        me.activeEl = el;
        me.setVisible("pendular-windows", el!=="pendular-main");
    },
    createFrame: function(key, href){
        frame = document.createElement("iframe");
        frame.setAttribute("src", href);
        frame.setAttribute("id", key);
        frame.setAttribute("class", "pendular-result-frame");
        return frame;
    },
    openLink: function(link){
        var me = this, windowList = me.windowList,
            key = link.title, href = link.href, frame;
        frame = YAHOO.util.Dom.get(key);
        if (!frame) { 
            var p = link.parentNode,
                isDir = (key==="") || YAHOO.util.Dom.hasClass(p, "pendular-result-type-directory")
            ;
            if (p) {
                p = p.parentNode
            }
            if (
                (isDir && YAHOO.util.Dom.hasClass(p, "pendular-folderview-contents")) ||
                YAHOO.util.Dom.hasClass(p, "pendular-folderview")
            ) {
                if (isDir) {
                    p = p.parentNode;
                }
                windowListEntry = windowList[p.id];
                delete windowList[p.id];
                me.onCloseWindow.fire(p.id, windowListEntry, windowListEntry.folderView.getEl());
                windowListEntry.folderView.setPath(key);
                windowListEntry.key = key;
                windowList[key] = windowListEntry;
                me.onOpenWindow.fire(key, windowListEntry, windowListEntry.folderView.getEl(), link);
                me.activeEl = key;
                frame = p;
            }
            else {
                windowListEntry = {
                    key: key,
                    next: me.head,
                    prev: me.tail
                };
                if (isDir) {
                    var fv = windowListEntry.folderView = new YAHOO.pendular.RepositoryFolderView({
                        path: key
                    });
                    frame = fv.init();
                }
                else {
                    frame = me.createFrame(key, href);
                }
                me.tail.next = windowListEntry;
                me.head.prev = windowListEntry;
                me.tail = windowList[key] = windowListEntry;
                YAHOO.util.Dom.get("pendular-windows").appendChild(frame);
                me.onOpenWindow.fire(key, windowListEntry, frame, link);
            }
        }
        this.setActiveEl(key);
    },
    close: function(key){
        var me = this,
            windowList = me.windowList,
            windowListEntry = windowList[key];
        if (me.tail===windowListEntry){
            me.tail = windowListEntry.next;
        }
        windowListEntry.prev.next = windowListEntry.next;
        windowListEntry.next.prev = windowListEntry.prev;
        me.setActiveEl(windowListEntry.prev.key);
        var frame = YAHOO.util.Dom.get(key);
        me.onCloseWindow.fire(key, windowListEntry, frame);
        frame.parentNode.removeChild(frame);
        delete windowList[key];
    },
});

})();
